package Thread;

import java.util.Scanner;

/**
 * 多个notify 同时唤醒一个线程
 */
public class Demo26 {
    //锁对象
    private static Object locker = new Object();

    public static void main(String[] args) {
        //线程1
        Thread t1 = new Thread(()->{
            synchronized (locker){
                System.out.println("t1 wait 之前");
                try{
                    locker.wait();
                }catch (InterruptedException e){
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait 之后");
            }
        });

        //线程2
        Thread t2 = new Thread(()->{
           synchronized (locker) {
               System.out.println("t2 wait 之前");
               try {
                   locker.wait();
               }catch (InterruptedException e){
                   throw new RuntimeException(e);
               }
               System.out.println("t2 wait 之后");
           }
        });

        //线程3
        Thread t3 = new Thread(()->{
           synchronized (locker){
               System.out.println("t3 wait 之前");
               try {
                   locker.wait();
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               System.out.println("t3 wait 之后");
           }
        });

        //线程4:用于唤醒前三个线程中的wait
        Thread t4 = new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            scanner.next();//必须要先输入一些内容使得程序继续运行
            System.out.println("t4 notify 之前");
            synchronized (locker){
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
                locker.notify();
            }
            System.out.println("t4 notify 之后");
        });

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}
/**
     无论最后多少个notify(这个notify的个数比wait要多),都只会唤醒对应次数个wait
 */

//补充一点,notifyAll可以一次性唤醒全部的wait